<?php

/**
 * CMCodeSet form.
 *
 * @package    form
 * @subpackage CMCodeSet
 * @version    SVN: $Id: sfPropelFormTemplate.php 6174 2007-11-27 06:22:40Z fabien $
 */
class PluginCMCodeSetForm extends BaseFormDoctrine
{   
  public function setup()
  {
    $this->setWidgets(array(
      'code_set_nm'         => new sfWidgetFormInputHidden(),
      'effective_from_time' => new sfWidgetFormI18nDateTime(array('culture' => 'en')),
      'status_cd'           => new rhWidgetGenCodeSelect(
  						                   array('code_set_nm'=>'Code_STS', 'add_empty'=>false)),
      'status_time'         => new sfWidgetFormInputHidden(),
      'class_cd'            => new rhWidgetGenCodeSelect(
  						                   array('code_set_nm'=>'Code_CLS', 'add_empty'=>false)),
      'effective_to_time'   => new sfWidgetFormI18nDateTime(array('culture' => 'en')),
    ));

    $this->setValidators(array(
      'code_set_nm'         => new sfValidatorDoctrineChoice(array('model' => 'CMCodeSet', 'column' => 'code_set_nm', 'required' => false)),
      'effective_from_time' => new sfValidatorDateTime(),
      'status_cd'           => new sfValidatorString(array('max_length' => 20)),
      'status_time'         => new sfValidatorPass(),
      'class_cd'            => new sfValidatorString(array('max_length' => 15, 'required' => false)),
      'effective_to_time'   => new sfValidatorDateTime(array('required' => false)),
    ));

    $this->widgetSchema->setNameFormat('cm_code_set[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    if ($this->isNew())
    {
      $this->widgetSchema['code_set_nm'] = new sfWidgetFormInput();
      $this->validatorSchema['code_set_nm']= new sfValidatorAnd(array(
        new sfValidatorCallback(
          array(
            'callback' => array($this, 'duplicateCheckValidator')),
          array('invalid'  => 'This Code Set Name already exists.')
        ),
        new sfValidatorString(array('min_length' => 4, 'max_length'=> 36), 
          array('invalid'=> 'Code set name must be between 4 and 36 characters.'))
        ));
  	}
  	$this->validatorSchema->setPostValidator(
  	  //new sfValidatorOr(array(
      new sfValidatorSchemaCompare('effective_from_time', sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'effective_to_time',
          array('throw_global_error' => false),
          array('invalid' => 'The effective from time ("%left_field%") must be before effective to time ("%right_field%")'))
      //new sfValidatorCallback(array('callback'=>array($this, 'onlyFromTimeValidator')), 
      //    array('invalid'  => 'Effective From Time must also be set.'))          
      // ))
      );
    
    parent::setup();
    
  }
  public function duplicateCheckValidator($validator, $value) 
  {
    $query = CMCodeSet::CodeSetNameExists($value);
    
    if($query)
    {
      throw new sfValidatorError($validator, 'invalid'); // Code_set_nm already exists
    } 
    else 
    {
      return $value; // Code_set_nm does not exist
    }
  }
  /*
   * Does not work as expected....
   */
  public function onlyFromTimeValidator($validator, $value) 
  {
    $eft = $value['effective_from_time'];
    $ett = $value['effective_to_time'];
    
    if(($eft == '' && $ett != '') && ($eft <= $ett))
    {
      throw new sfValidatorError($validator, 'invalid'); // Code_set_nm already exists
    } 
    else 
    {
      return $value; // Code_set_nm does not exist
    }
  }

  public function getModelName()
  {
    return 'CMCodeSet';
  }
}
